package com.mayikt.controller;

import com.mayikt.req.UserLoginReq;
import com.mayikt.req.UserQueryReq;
import com.mayikt.req.UserResetPasswordReq;
import com.mayikt.req.UserSaveReq;
import com.mayikt.resp.CommonResp;
import com.mayikt.resp.UserLoginResp;
import com.mayikt.resp.UserQueryResp;
import com.mayikt.resp.PageResp;
import com.mayikt.service.UserService;
import com.mayikt.utils.RedisTemplateUtils;
import com.mayikt.utils.SnowFlake;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private SnowFlake snowFlake;
    @Resource
    private RedisTemplateUtils redisTemplateUtils;

    @GetMapping("/list")
    public CommonResp list(UserQueryReq userQueryReq) {
        CommonResp<PageResp<UserQueryResp>> resp = new CommonResp<>();
        PageResp<UserQueryResp> list = userService.list(userQueryReq);
        resp.setData(list);
        return resp;
    }

    @PostMapping("/save")
    public CommonResp save(@Valid @RequestBody UserSaveReq userSaveReq) {
        // 密码进行dm5的32位16进制加密
        userSaveReq.setPassword(DigestUtils.md5DigestAsHex(userSaveReq.getPassword().getBytes()));
        CommonResp resp = new CommonResp<>();
        int result = userService.save(userSaveReq);
        if(result < 1) {
            resp.setSuccess(false);
        }
        return resp;
    }

    @PostMapping("/reset-password")
    public CommonResp resetPassword(@Valid @RequestBody UserResetPasswordReq userResetPasswordReq) {
        // 密码进行dm5的32位16进制加密
        userResetPasswordReq.setPassword(DigestUtils.md5DigestAsHex(userResetPasswordReq.getPassword().getBytes()));
        CommonResp resp = new CommonResp<>();
        int result = userService.resetPasswork(userResetPasswordReq);
        if(result < 1) {
            resp.setSuccess(false);
        }
        return resp;
    }

    @DeleteMapping("/delete/{id}")
    public CommonResp delete(@PathVariable Long id) {
        CommonResp resp = new CommonResp<>();
        int result = userService.delete(id);
        if(result < 1) {
            resp.setSuccess(false);
        }
        return resp;
    }

    @PostMapping("/login")
    public CommonResp resetPassword(@Valid @RequestBody UserLoginReq userLoginReq) {
        // 密码进行dm5的32位16进制加密
        userLoginReq.setPassword(DigestUtils.md5DigestAsHex(userLoginReq.getPassword().getBytes()));
        CommonResp resp = new CommonResp<>();
        UserLoginResp user = userService.login(userLoginReq);

        Long token = snowFlake.nextId();
        log.info("生成单点登录token：" + token);
        user.setToken(token.toString());
        redisTemplateUtils.setObject(token.toString(), user, 3600 * 24L);
        resp.setData(user);
        return resp;
    }

    /**
     * 退出登录
     * @param token
     * @return
     */
    @GetMapping("/logout/{token}")
    public CommonResp logout(@PathVariable String token) {
        CommonResp resp = new CommonResp<>();
        log.info("退出登录" + token);
        redisTemplateUtils.deleteObject(token);
        return resp;
    }
}
